#region Using Statements
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;
#endregion

namespace SpaceKinect
{
    /// <summary>
    /// The options screen is brought up over the top of the main menu
    /// screen, and gives the user a chance to configure the game
    /// </summary>
    class OptionsMenuScreen : MenuScreen
    {
        #region Fields
        MenuEntry DisplayResolutionMenuEntry;
        MenuEntry MusicMenuEntry;
        MenuEntry InvertYMenuEntry;
        MenuEntry KinectMenuEntry;

        //Display resolution
        static string[] DisplayResolution = { "1360X768", "Full Screen"};
        static int currentResolution = 0;
        //Those varibles shared by all the class 
        public static bool fullSize = false;
        public static bool audioON = true;
        public static bool invertY = false;
        public static bool kinectOff = false;

        #endregion

        #region Initialization


        /// <summary>
        /// Constructor.
        /// </summary>
        public OptionsMenuScreen()
            : base("Options")
        {
            // Create our menu entries.
            DisplayResolutionMenuEntry = new MenuEntry(string.Empty);
            MusicMenuEntry = new MenuEntry(string.Empty);
            InvertYMenuEntry = new MenuEntry(string.Empty);
            KinectMenuEntry = new MenuEntry(string.Empty);
            SetMenuEntryText();

            MenuEntry back = new MenuEntry("Back");

            // Hook up menu event handlers.
            DisplayResolutionMenuEntry.Selected += DisplayResolutionMenuEntrySelected;
            MusicMenuEntry.Selected += AudioMenuEntrySelected;
            InvertYMenuEntry.Selected += InvertYMenuEntrySelected;
            KinectMenuEntry.Selected += KinectMenuEntrySelected;
            back.Selected += OnCancel;
            
            // Add entries to the menu.
            MenuEntries.Add(DisplayResolutionMenuEntry);
            MenuEntries.Add(MusicMenuEntry);
            MenuEntries.Add(InvertYMenuEntry);
            MenuEntries.Add(KinectMenuEntry);
            MenuEntries.Add(back);
        }


        /// <summary>
        /// Fills in the latest values for the options screen menu text.
        /// </summary>
        void SetMenuEntryText()
        {
            //ungulateMenuEntry.Text = "Preferred ungulate: " + currentResolution;
            DisplayResolutionMenuEntry.Text = "DisplayResolution: " + DisplayResolution[currentResolution];
            MusicMenuEntry.Text = "Music: " + (audioON ? "on" : "off");
            InvertYMenuEntry.Text = "InvertY: " + (invertY ? "on" : "off");
            KinectMenuEntry.Text = "Kinect: " + (kinectOff ? "on" : "off");
        }


        #endregion

        #region Handle Input


        /// <summary>
        /// Event handler for when the Display Resolution menu entry is selected.
        /// </summary>
        void DisplayResolutionMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            currentResolution = (currentResolution + 1) % DisplayResolution.Length;
            fullSize = !fullSize;
            //Changing the size of the display
            if (fullSize == true)
            {
                Program.GameMenu.graphics.IsFullScreen = true;
                Program.GameMenu.graphics.ApplyChanges();
            }
            else
            {
                Program.GameMenu.graphics.IsFullScreen = false;
                Program.GameMenu.graphics.ApplyChanges();
            }
            SetMenuEntryText();
        }

        /// <summary>
        /// Event handler for when the Audio menu entry is selected.
        /// </summary>
        void AudioMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            audioON = !audioON;
            MediaPlayer.IsMuted = !audioON;
            SetMenuEntryText();
        }

        /// <summary>
        /// Event handler for when the Kinect menu entry is selected.
        /// </summary>
        void KinectMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            kinectOff = !kinectOff;

            SetMenuEntryText();
        }


        /// <summary>
        /// Event handler for when the InvertY menu entry is selected.
        /// </summary>
        void InvertYMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            invertY = !invertY;

            SetMenuEntryText();
        }

        #endregion
    }
}
